<?php
/**
* @Copyright Copyright (C) 2010- ... Andy Sharman
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
**/
$doc =& JFactory::getDocument();
$doc->addStyleSheet(JURI::base(false).'/components/com_customfields/customfields.css' );


//add new
if (JRequest::getVar('txtAddCustomFieldValue'))
{
	$CF->addValue(htmlentities(JRequest::getString('cboFieldId'),ENT_QUOTES),JRequest::getString('article_id'),JRequest::getString('txtAddCustomFieldValue'));
}


//output form + existing.
if ($CF->countFields() >= 0)
{
	?>
	<div id="addCustomFieldValue">
		<form name="frmAddFields" id="frmAddFields" method="post" action="" target="_self">			
			<p>
				<select name="cboFieldId">
					<?php
						$fields = $CF->listFields();
						foreach ($fields as $field)
						{
							echo '<option value="'.$field->id.'">'.$field->name.'</option>';	
						}
					?>
				</select>
				<textarea name="txtAddCustomFieldValue" cols="15" rows="5"></textarea>
			</p>
			<input type="hidden" name="article_id" value="<?php echo JRequest::getVar('article_id'); ?>" />
			<input type="submit" class="btnAdd" name="btnSubmit" value="Add" />		
		</form>
	</div>
	
	<?php
	
	
if (JRequest::getVar('updateId'))
{
	$CF->updateValue(JRequest::getString('updateId'),
						JRequest::getString('cboFieldId'),
						JRequest::getVar( 'txtUpdateFieldValue', '', 'post', 'string', JREQUEST_ALLOWHTML ));
}

if (JRequest::getVar('deleteId'))
{
	$CF->deleteValue(JRequest::getString('deleteId'));
}

if ($CF->countValues(JRequest::getVar('article_id')) >= 0)
{
	$values = $CF->listValues(JRequest::getVar('article_id'));
	
	foreach ($values as $value)
	{
		echo '<div class="customValueListItem">';
			echo '<form name="frmUpdateField'.$value->id.'" class="updateForm" method="post" action="">';
				echo '<input type="hidden" name="updateId" value="'.$value->id.'" />';
				echo '<select name="cboFieldId">';
				$fields = $CF->listFields();
				foreach ($fields as $field)
				{
					if ($field->id == $value->field_id)
					{
						echo '<option value="'.$field->id.'" selected="selected">'.$field->name.'</option>';	
					}
					else
					{
						echo '<option value="'.$field->id.'">'.$field->name.'</option>';	
					}
				}
				echo '</select> ';
				echo '<textarea name="txtUpdateFieldValue" cols="15" rows="5">'.$value->value.'</textarea>';
				echo '</p>';		
				echo '<input type="submit" class="btnAmend" name="btnUpdate" value="Update" />';
			echo '</form>';
			
			echo '<form name="frmDeleteField'.$value->id.'" class="delete" method="post" action="">';
				echo '<input type="hidden" name="deleteId" value="'.$value->id.'" />';
				echo '<input type="submit" class="btnDelete" name="btnDelete" value="Delete" />';
			echo '</form>';
		echo '</div>';	
	}
	
}
	

}		
else
{
	echo '<h1>Error :: Custom field types need to be added first before values can be added.</h1>';
}